// 智能锁列表
import debounce from "debounce";
import * as MSroomAPI from "../../api/interfaces/MSroom";
import { HttpHandler } from "../../api/handle/httpHandler";
import { AES_Encrypt } from "../../utils/crypto";

Page({
  data: {
    roomList: [],

  },
  onLoad() {

  },

  onShow() {
    this.getTokens()
    if (wx.getStorageSync('hotelId')) {
      this.getRoomList(wx.getStorageSync('hotelId'))
    }
    const eventChannel = this.getOpenerEventChannel();
    this.eventChannel = eventChannel
    eventChannel.on('hotelInfo', (data) => {
      this.setData({ hotelInfo: data.item });
      console.log(data.item);
      wx.setStorageSync('hotelId', data.item.id)
      this.getRoomList(data.item.id)
    });
    this.setData({ role: wx.getStorageSync('role') });
    if (this.data.role.includes('system_admin')) {
      this.setData({ hasButton: true });
    } else {
      this.setData({ hasButton: false });
    }
  },
  getTokens() {
    MSroomAPI.getToken().then(res => {
      if (res.code == 200) {
        wx.removeStorageSync("access_token"); // 清空当前用户保存的登录状态
        wx.setStorageSync("access_token", AES_Encrypt(res.result));
      }
    })
  },
  lookPwd(item) {
    MSroomAPI.getLockDetail({
      lockId: item.currentTarget.dataset.query.lockTtId
    }).then(res => {
      if (res.result) {
        console.log(JSON.parse(res.result));
        wx.showModal({
          title: '房间密码',
          content: JSON.parse(res.result).noKeyPwd,
          showCancel: false,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })

      }
    })
  },
  lookQrCode(item) {
 console.log(item.currentTarget.dataset.query);

 MSroomAPI.getRoomRcCode({
   roomId:item.currentTarget.dataset.query.id
 }).then(res=>{
if(res.code==200){
 wx.previewImage({

  urls: ['https://lyms.imsdx.net/lyms/'+res.result] // 需要预览的图片http链接列表
})
}
 })

  },
  // 获取房间
  getRoomList(hotelId) {
    MSroomAPI.getRoomByHotelId({
      hotelId: hotelId
    }).then(res => {
      if (res.result) {
        console.log(res);
        let demo = res.result.records
        demo.forEach(e => {
          e.cover = 'https://lyms.imsdx.net/lyms/' + e.cover
        });
        this.setData({ roomList: res.result.records });
      }
    })
  },
  // 进入酒店详情
  toDetail(e) {

    wx.navigateTo({
      url: "/pages/initLock/initLock?roomId=" + e.target.dataset.query.id,
    })

  },


  /* TODO 处理用户错误日志, 用户可自行操作日志上传 */
  handleShowLog(...args: any) {
    console.log("操作日志:", ...args);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // this.modifyKeyList();
  },

  onReachBottom() {
    // this.modifyKeyList(this.data.currentPageNo + 1);
  },







})